package game.metalmax.module.config
{
	import game.metalmax.entity.main.Dialog;
	import game.metalmax.entity.main.DialogPhase;
	import game.metalmax.entity.server.ServerConfig;
	
	import showtime.framework.ai.AiConfig;
	import showtime.framework.core.pattern.Singleton;
	import showtime.framework.engine.animation.Animation;
	import showtime.framework.engine.config.AnimationConfig;
	import showtime.framework.manager.AssetManager;
	import showtime.framework.manager.DataBaseManager;
	import showtime.framework.manager.StyleManager;
	import showtime.framework.manager.supportClasses.style.Style;

	public class ConfigManager extends Singleton
	{
		public static function getInstance():ConfigManager
		{
			return ConfigManager(Singleton.getInstanceOrCreate(ConfigManager));
		}
		
		override protected function initialize():void
		{
			extractAnimationConfig();
			
			extractStyleConfig();
			
			extractDialogConfig();
			
			extractServerConfig();
			
			extractAiConfig();
		}
		
		private function extractServerConfig():void
		{
			var xml:XMLList = XML(AssetManager.getInstance().getOBJ("serverConfig")).elements("socket");
			
			var serverConfig:ServerConfig = null;
			for each (var element:XML in xml)
			{
				serverConfig = new ServerConfig();
				serverConfig.id = element.@name;
				serverConfig.ip = element.@ip;
				serverConfig.port = element.@port;
				
				DataBaseManager.getInstance().writeOrUpdate(serverConfig);
			}
		}
		
		private function extractAnimationConfig():void
		{
			var xml:XMLList = XML(AssetManager.getInstance().getOBJ("animationConfig")).elements("animations");
		
			var animationConfig:AnimationConfig = null;
			
			for each (var element:XML in xml)
			{
				animationConfig = new AnimationConfig(element.@id);
				
				for each (var _element:XML in element.elements("animation"))
				{
					animationConfig.animations.push(new Animation(_element.@name, String(_element.@frames).split(","), _element.@frameRate, false));					
				}
				
				DataBaseManager.getInstance().writeOrUpdate(animationConfig);
			}
		}
		
		private function extractStyleConfig():void
		{
			var xml:XMLList = XML(AssetManager.getInstance().getOBJ("styleConfig")).elements("style");			
			var style:Style = null;
			
			for each (var element:XML in xml)
			{
				style = new Style();
				style.id = element.@id;
				style.color = element.@color;
				style.font = element.@font;
				style.size = element.@size;
				style.bold = element.@bold == "true" ? true : false;
				
				StyleManager.getInstance().registerStyle(style);
			}
		}
		
		private function extractDialogConfig():void
		{
			var xml:XMLList = XML(AssetManager.getInstance().getOBJ("dialogConfig")).elements("dialog");			
			var dialog:Dialog = null;
			var phase:DialogPhase = null;
			
			for each (var element:XML in xml)
			{
				dialog = new Dialog();
				dialog.id = element.@id;
				dialog.startIndex = element.@startIndex;
				dialog.phases = [];
				
				for each (var _element:XML in element.elements("phase"))
				{
					phase = new DialogPhase();
					phase.accept = _element.@accept;
					phase.refuse = _element.@refuse;
					phase.index = _element.@index;
					phase.type = _element.@type;
					phase.content = _element;
					phase.scriptID = _element.@scriptID;
					
					dialog.phases.push(phase);

				}
				
				DataBaseManager.getInstance().writeOrUpdate(dialog);
			}
		}
		
		private function extractAiConfig():void
		{
			var xml:XMLList = XML(AssetManager.getInstance().getOBJ("aiConfig")).elements("ai");			
			var ai:AiConfig = null;
			
			for each (var element:XML in xml)
			{
				ai = new AiConfig();
				ai.id = element.@id;
				ai.intentionType = element.@intentionType;
				ai.direction = element.@direction;
				ai.interval = element.@interval;
				
				DataBaseManager.getInstance().writeOrUpdate(ai);
			}
		
		}
		
	}
}